/**
 * 
 */
package uno.component;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;

import uno.net.UnoPlay;
import uno.net.UnoPlayer;
import uno.utilities.URNResolver;

/**
 * @author mattialambertini
 *
 */
public class ChangeColorCard extends Card {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3879403494746404938L;


	/**
	 * @param width
	 * @param height
	 * @param value
	 * @param parent
	 */
	public ChangeColorCard(int width, int height, CardValue value,
			HandOfCard parent) {
		super(width, height, value, parent);
		
	}

	
	/* (non-Javadoc)
	 * @see uno.component.Card#addCardListeners()
	 */
	@Override
	protected void addCardClickListeners() {
		
		//super.addCardMovesListeners();
		
		addMouseListener(new MouseAdapter(){
			
			public void mouseClicked(MouseEvent e) {
				
				super.mouseClicked(e);
				Card tmpCard = (Card)e.getSource();
				
				HandOfCard hocParent = getParent(); 
				hocParent.removeCard(tmpCard);//rimuovo dalla mano del giocatore la carta
				hocParent.repaint();
				clickBehavior(hocParent, tmpCard);
				
			}
			
			/**
			 * Metodo in cui e' implementato il comportamento di questa carta quando 
			 * viene giocata.
			 * */
			private void clickBehavior(HandOfCard hocParent, Card playedCard){
				
				System.out.println("Cambio il colore corrente a mio piacimento");
				String args[] = {CardValue.YELLOW, CardValue.GREEN, CardValue.RED, CardValue.BLUE};
				
				//faccio apparire un pop-up per scegliere il colore 
				int choice = JOptionPane.showOptionDialog(null, "Choose color:", "Color Dialog", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, args, null);
				//System.out.println(choice);
				UnoPlayer currentPlayer = hocParent.getOwner();
				
				//creo l'oggetto mossa
				UnoPlay play = null;
				/*
				 * Se gioco un carta e basta la dimensione del mazzo
				 * non varia ma se prima ho prelevato una carta allora varia
				 * ..bisogna verificare anche questo
				 * */
				int deckSize = currentPlayer.getDeck().getDeckSize();
				
				//controlla la scelta dell'utente
				//potrei anche ciclare finche' non la smette di premere la x
				//chiudendo la finestra senza chiuderla.. ma anche no.
				switch(choice) {
				
				case 0: 
					play = new UnoPlay(currentPlayer.getGameStatus().getID() + 1, currentPlayer.getMyPlayerID(), URNResolver.cardToURN(playedCard), deckSize, CardValue.YELLOW, currentPlayer.getHandOfCard().getHOCSize());
					break;
				case 1: 
					play = new UnoPlay(currentPlayer.getGameStatus().getID() + 1, currentPlayer.getMyPlayerID(), URNResolver.cardToURN(playedCard), deckSize, CardValue.GREEN, currentPlayer.getHandOfCard().getHOCSize());
					break;
				case 2: 
					play = new UnoPlay(currentPlayer.getGameStatus().getID() + 1, currentPlayer.getMyPlayerID(), URNResolver.cardToURN(playedCard), deckSize, CardValue.RED, currentPlayer.getHandOfCard().getHOCSize());
					break;
				case 3: 
					play = new UnoPlay(currentPlayer.getGameStatus().getID() + 1, currentPlayer.getMyPlayerID(), URNResolver.cardToURN(playedCard), deckSize, CardValue.BLUE, currentPlayer.getHandOfCard().getHOCSize());
				
				}
				
				//passo la mossa al giocatore che l'ha giocata in modo tale che possa gestirla
				currentPlayer.updateStatus(play);
				
				
			}

		});
	}
	
}
